
const { ccclass, property } = cc._decorator;

@ccclass
export class DropNode extends cc.Component {
    private _clickCall: () => void;
    nodePos: cc.Vec3;
    _minX: number;
    _maxX: number;
    _minY: number;
    _maxY: number;
    _touchMoved: boolean;

    set onClickCall(clickCall: () => void) {
        this._clickCall = clickCall
    }

    onLoad() {
        //节点初始位置,每次触摸结束更新
        this.node.getPosition(this.nodePos);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
    }

    //触摸移动；
    onTouchMove(event: cc.Event.EventTouch) {
        var self = this;
        var n_pos = event.getLocation();
        var delta = event.getDelta();

        self.node.x += delta.x;
        self.node.y += delta.y;

        // 控制节点移不出屏幕; 
        this._minX = -self.node.parent.width / 2 + self.node.width / 2; //最小X坐标；
        this._maxX = Math.abs(this._minX);
        this._minY = -self.node.parent.height / 2 + self.node.height / 2; //最小Y坐标；
        this._maxY = Math.abs(this._minY);
        var nPos = self.node.getPosition(); //节点实时坐标；

        if (nPos.x < this._minX) {
            nPos.x = this._minX;
        };
        if (nPos.x > this._maxX) {
            nPos.x = this._maxX;
        };
        if (nPos.y < this._minY) {
            nPos.y = this._minY;
        };
        if (nPos.y > this._maxY) {
            nPos.y = this._maxY;
        };
        self.node.setPosition(nPos);


        let touch = event.touch;
        let deltaMove = touch.getLocation().sub(touch.getStartLocation());
        if (deltaMove.mag() > 7) {//设置标志位，不触发touchend
            this._touchMoved = true;
        }
    }
    onTouchEnd(event: cc.Event.EventTouch) {
        if (this._touchMoved) {
            this.node.getPosition(this.nodePos); //获取触摸结束之后的node坐标；
            // 吸附到最近的边
            let leftOffset = this.nodePos.x - this._minX,
                rightOffset = this._maxX - this.nodePos.x,
                topOffset = this._maxY - this.nodePos.y,
                bottomOffset = this.nodePos.y - this._minY
            switch (Math.min(leftOffset, rightOffset, topOffset, bottomOffset)) {
                case leftOffset:
                    this.nodePos.x = this._minX
                    break
                case rightOffset:
                    this.nodePos.x = this._maxX
                    break
                case topOffset:
                    this.nodePos.y = this._maxY
                    break
                case bottomOffset:
                    this.nodePos.y = this._minY
                    break

                default:
                    break
            }
            this.node.stopAllActions()
            cc.tween(this.node)
                .to(0.1, { position: this.nodePos })
                .call(() => {
                    this.node.getPosition(this.nodePos)
                })
                .start()
        } else {
            this._clickCall()
        }
    }

    onTouchCancel() {
        this.node.getPosition(this.nodePos); //获取触摸结束之后的node坐标；
    }

    onTouchStart() {
        this._touchMoved = false
    }

}